﻿@using ABC.Controllers.ViewModels
@using ABC.Controllers.ViewModels.My
@model CalculationsViewModel
@{
    ViewBag.Title = "Сохраненные расчеты - Азбука24";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}
<script type="text/javascript">
    $(function () {
        var allCheckBoxes = $("input[name='calculation']").add("input[name='objectInsurance']");
        $("input[name='objectInsurance']").bind("change", function (e) {
            if ($(e.target).is(":checked")) {
                $("input[group='" + $(e.target).attr("group") + "']").attr("checked", "checked");
            }
            else {
                $("input[group='" + $(e.target).attr("group") + "']").removeAttr("checked");
            }
            if (allCheckBoxes.not(":checked").length > 0) {
                $("input[name='header']").removeAttr("checked");
            }
            else {
                $("input[name='header']").attr("checked", "checked");
            }
        });
        $("input[name='calculation']").bind("change", function (e) {
            var allCheckBoxesInGroup = $("input[name='calculation'][group='" + $(e.target).attr("group") + "']");
            var notCheck = allCheckBoxesInGroup.not(":checked");
            if (notCheck.length > 0) {
                $("input[name='objectInsurance'][group='" + $(e.target).attr("group") + "']").removeAttr("checked");
                $("input[name='header']").removeAttr("checked");
            }
            else {
                $("input[name='objectInsurance'][group='" + $(e.target).attr("group") + "']").attr("checked", "checked");
            }
            if (allCheckBoxes.not(":checked").length == 0) {
                $("input[name='header']").attr("checked", "checked");
            }
        });
        $("input[name='header']").bind("change", function (e) {
            if ($("input[name='header']").is(":checked")) {
                allCheckBoxes.attr("checked", "checked");
            }
            else {
                allCheckBoxes.removeAttr("checked");
            }
        });
    });
    function ChangePageSize() {
        location.href = '@Url.Action("Calculations", "Cabinet")' + ($("#PageSize").val() != '@Model.ListForPageSize.First().Value' ? "?pagesize=" + $("#PageSize").val() : "");
    }
    function DeleteCalculation(obj) {
        var chk = $(obj).parents("tr").children("td").children("input[type='checkbox']");
        var form1 = document.createElement('form');
        form1.action = '@Url.Action("DeleteCalculation", "Cabinet")';
        form1.method = 'post';
        form1.acceptCharset = 'utf-8';
        form1.appendChild(CreateHiddenField('id', chk.val()));
        form1.appendChild(CreateHiddenField('page', '@Model.PageNumber'));
        form1.appendChild(CreateHiddenField('pagesize', ('@Model.PageSize' == '0' ? "" : '@Model.PageSize')));
        document.body.appendChild(form1);
        form1.submit();
    }
</script>
<div class="row-fluid">
    @{Html.RenderPartial("_SectionCabinetMenu", new BaseViewModel());}
    <div class="span9">
        <div class="page-header">
            <h2>
                Сохраненные расчеты</h2>
        </div>
        <table class="table table-bordered table-striped" id="mycalculate">
            <thead>
                <tr>
                    <th class="col-1">
                        <input type="checkbox" name="header" id="inlineCheckbox01" value="option0" title="Выделить все">
                    </th>
                    <th>
                        Номер
                    </th>
                    <th>
                        Дата расчета
                    </th>
                    <th>
                        Риск
                    </th>
                    <th>
                        Управление
                    </th>
                </tr>
            </thead>
            <tbody>
                @using (Html.BeginForm("DeleteCalculations", "Cabinet", FormMethod.Post, new { id = "form1" }))
                {
                    @Html.Hidden("page", Model.PageNumber)
                    @Html.Hidden("pagesize", Model.PageSize == 0 ? "" : Model.PageSize.ToString())
                    for (var i = 0; i < Model.Calculations.Count; i++)
                    {
                        if (i == 0)
                        {
                    <tr>
                        <th>
                            <input type="checkbox" name="objectInsurance" group="@string.Format("{0}{1}{2}", Model.Calculations[i].CarManufacture, Model.Calculations[i].CarModel, Model.Calculations[i].Release)" id="inlineCheckbox1" title="Отметить все в заданном периоде">
                        </th>
                        <th colspan="100%">
                            <a href="@Url.Action("GetObjectId", "Cabinet", new { carManufacture = Model.Calculations[i].CarManufacture, carModel = Model.Calculations[i].CarModel, carRelease = Model.Calculations[i].Release })" title="Показать объект страхования" target="_blank">
                                @string.Format("{0} {1}, {2}", Model.Calculations[i].CarManufacture, Model.Calculations[i].CarModel, Model.Calculations[i].Release)</a>
                        </th>
                    </tr>
                        }
                        else
                        {
                            if (Model.Calculations[i].CarManufacture != Model.Calculations[i - 1].CarManufacture || Model.Calculations[i].CarModel != Model.Calculations[i - 1].CarModel || Model.Calculations[i].Release != Model.Calculations[i - 1].Release)
                            {
                    <tr>
                        <th>
                            <input type="checkbox" name="objectInsurance" group="@string.Format("{0}{1}{2}", Model.Calculations[i].CarManufacture, Model.Calculations[i].CarModel, Model.Calculations[i].Release)" title="Отметить все в заданном периоде">
                        </th>
                        <th colspan="100%">
                            <a href="@Url.Action("GetObjectId", "Cabinet", new { carManufacture = Model.Calculations[i].CarManufacture, carModel = Model.Calculations[i].CarModel, carRelease = Model.Calculations[i].Release })" title="Показать объект страхования" target="_blank">
                                @string.Format("{0} {1}, {2}", Model.Calculations[i].CarManufacture, Model.Calculations[i].CarModel, Model.Calculations[i].Release)</a>
                        </th>
                    </tr>
                            }
                        }
                    <tr>
                        <td class="col-1">
                            <input type="checkbox" name="calculation" group="@string.Format("{0}{1}{2}", Model.Calculations[i].CarManufacture, Model.Calculations[i].CarModel, Model.Calculations[i].Release)" value="@Model.Calculations[i].Id" title="Отметить">
                        </td>
                        <td>
                            <a href="@(Model.Calculations[i].TypeCalculation == "КАСКО" ? Url.Action("Kasko", "Information", new { id = Model.Calculations[i].Id }) : Url.Action("OsagoResult", "Cabinet", new { id = Model.Calculations[i].Id }))">@Model.Calculations[i].UniqueNumber</a>
                        </td>
                        <td>
                            @Model.Calculations[i].CalculationDate.ToString("dd.MM.yyyy HH:mm")
                        </td>
                        <td>
                            @Model.Calculations[i].Risk
                        </td>
                        <td>
                            <div class="btn-group">
                                <button class="btn" rel="tooltip" data-original-title="Распечатать">
                                    <i class="icon-print"></i>
                                </button>
                                <button class="btn" type="button" rel="tooltip" data-original-title="Удалить" onclick="DeleteCalculation(this);">
                                    <i class="icon-trash"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    }
                }
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="100%">
                        <div class="table-toolbar">
                            <div class="quant-item pull-left">
                                @Html.DropDownListFor(x => x.PageSize, Model.ListForPageSize, new { @class = "input-mini", onchange = "ChangePageSize();" })
                                <span class="help-inline">записей на странице</span>
                            </div>
                            <div class="pagination pagination-right">
                                <ul>
                                    @Html.Paging(Model.PageNumber, Model.PageCount, j => Url.Action("Calculations", "Cabinet", new { page = j }))
                                </ul>
                            </div>
                        </div>
                    </td>
                </tr>
            </tfoot>
        </table>
        <div class="btn-toolbar">
            <button class="btn" type="button" onclick="$('#form1').submit();">
                Удалить</button>
        </div>
    </div>
</div>
